package mta.app;

import mta.command.CommandIF;
import mta.command.Help;
import mta.command.generate.GeneratePatchBuilder;
import mta.command.generate.GeneratePatchOptions;
import mta.command.merge.MergeBuilder;
import mta.command.merge.MergeOptions;
import mta.command.patch.PatchBuilder;
import mta.command.patch.PatchOptions;
import org.apache.commons.cli.ParseException;

/**
 * Magento Translation Assistant
 * Copyright (c) 2009 Dmitry Nikolaev
 * Contact author: blackliteon@gmail.com
 */
public class ApplicationBuilder {

    private String[] args;

    public ApplicationBuilder(String[] args) {
        this.args = args;
    }

    private CommandIF buildHelp(ApplicationBuilderOptions appBuilderOptions) {
        Help help = new Help();
        help.addOptions("mta operations", appBuilderOptions.getOptions());
        help.addOptions("merge localizations", new MergeOptions().getOptions());
        help.addOptions("generate patch file", new GeneratePatchOptions().getOptions());
        help.addOptions("patch localization", new PatchOptions().getOptions());

        return help;
    }

    public CommandIF createApplication() {
        ApplicationBuilderOptions builderOptions = new ApplicationBuilderOptions(args);

        try {
            switch (builderOptions.getCommand()) {

                case Patch:
                    return new PatchBuilder(args).build();
                case GeneratePatch:
                    return new GeneratePatchBuilder(args).buildPatchGenerator();
                case MergeLocalizations:
                    return new MergeBuilder(args).buildMergeCommand();
                default:
                    return buildHelp(builderOptions);

            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
